<template>
  <div class="container">
    <PageHeader title="用户管理" content="添加用户" />
    <el-card
      body-style=" height: calc(100vh - 200px);
    overflow-y: auto;"
    >
      <template #header>
        <div class="card-header">
          <span>添加用户信息</span>
        </div>
      </template>
      <el-form
        ref="userFormRef"
        style="max-width: 600px"
        :model="userAddForm"
        :rules="userFormRules"
        label-width="100px"
        class="userform-box"
        status-icon
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userAddForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="userAddForm.password"
            placeholder="请输入密码"
            show-password
            type="password"
          />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="userAddForm.gender" placeholder="请选择性别">
            <el-option
              v-for="item in genderOptions"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="userAddForm.role" placeholder="请选择角色">
            <el-option
              v-for="item in roleOptions"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input v-model="userAddForm.introduction" type="textarea" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <UploadFile :avatar="userAddForm.avatar" @handle-upload="handleUpload" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(userFormRef)"> 添加 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script setup lang="ts">
import type { AddUserInfo } from '@/types/user'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import UploadFile from '@/components/upload/UploadFile.vue'
import { ElMessage, type FormInstance, type UploadRawFile } from 'element-plus'
import upload from '@/utils/upload'
const roleOptions = [
  {
    label: '管理员',
    value: 1,
  },
  {
    label: '普通用户',
    value: 2,
  },
]
const genderOptions = [
  {
    label: '保密',
    value: 0,
  },
  {
    label: '男',
    value: 1,
  },
  {
    label: '女',
    value: 2,
  },
]
const router = useRouter()
const userFormRef = ref()
const userAddForm: AddUserInfo = reactive({
  username: '',
  password: '',
  gender: 0,
  introduction: '',
  role: 2, //默认为普通用户
  avatar: '',
  file: undefined,
  deleted: false,
})
const userFormRules = reactive({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'blur' }],
  introduction: [{ required: true, message: '请输入介绍', trigger: 'blur' }],
  avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }],
})
const handleUpload = (uploadFile: UploadRawFile) => {
  console.log(uploadFile)
  userAddForm.avatar = URL.createObjectURL(uploadFile)
  userAddForm.file = uploadFile
}

/**
 * 提交表单
 *
 * @param formEl 表单实例
 */
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  userFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      const res = await upload('/adminapi/user/add', userAddForm)
      const { code, msg } = res
      if (code === '0') {
        router.push('/user-manage/userlist')
      } else {
        ElMessage.error(msg)
      }
    } else {
      console.log('表单校验失败!')
      return false
    }
  })
}
</script>
<style lang="scss" scoped>
.container {
  .el-form {
    .el-form-item {
      margin: 20px auto;
    }
  }
}
</style>
